﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TEKVR05
    {
        #region ==IsFilterByVKP===========
        public static readonly DependencyProperty IsFilterByVKPProperty = DependencyProperty.Register("IsFilterByVKP",
            typeof(bool), typeof(TEKVR05),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFilterByVKPChanged));
        public bool IsFilterByVKP
        {
            get
            {
                return (bool)GetValue(IsFilterByVKPProperty);
            }
            set
            {
                SetValue(IsFilterByVKPProperty, value);
            }
        }
        private static void OnIsFilterByVKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TEKVR05;
            if (src != null)
            {
                src.IsFilterByVKP = (bool)e.NewValue;
            }
        }
        #endregion 

        #region ==dwZB===========
        public static readonly DependencyProperty dwZBProperty = DependencyProperty.Register("dwZB",
            typeof(DataRow), typeof(TEKVR05),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwZBChanged));
        public DataRow dwZB
        {
            get
            {
                return (DataRow)GetValue(dwZBProperty);
            }
            set
            {
                SetValue(dwZBProperty, value);
            }
        }
        private static void OndwZBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TEKVR05;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwZB = row;
            }
        }
        #endregion

        #region ==dwEKP===========
        public static readonly DependencyProperty dwEKPProperty = DependencyProperty.Register("dwEKP",
            typeof(DataRow), typeof(TEKVR05),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwEKPChanged));
        public DataRow dwEKP
        {
            get
            {
                return (DataRow)GetValue(dwEKPProperty);
            }
            set
            {
                SetValue(dwEKPProperty, value);
                LOADBYEKP();
            }
        }
        private static void OndwEKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TEKVR05;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwEKP = row;
            }
        }
        #endregion

        public TEKVR05()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVRPOS();
            DataContext = MV;
            InitializeComponent();
        }

        public override void LOADBYRENR(string value)
        {
            base.LOADBYRENR(value);
            if (value.Length < 8)
                return;
            if (!IsAuto)
                return;
            return;
            if (TBMain.Rows.Count > 0)
                return;
            dwData = TBMain.NewRow();
            dwData["VRPMANDANT"] = app.Mandant;
            dwData["VRPRENR"] = value.Substring(0, 8);
            ((VVRPOS)MV).GetPOS(dwData, 9);
            dwData["VRPREPOS"] = POS;
            dwData["VRPGESWERT"] = 0;
            ((VVRPOS)MV).Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
        }

        public void LOADBYEKP()
        {
            TBMain.Clear();
            if (dwEKP == null)
                return;
            if (dwZB == null)
                return;
            EKP ="B"+ dwEKP["EKPAUFKRS"].ToString() + dwEKP["EKPAUFLFD"].ToString() + dwEKP["EKPAUFPOS"].ToString();
            if (!IsAuto)
                return;

            filter = " VRPRENR='" + RENR + "' AND VRPAUFKRS='" + EKP.Substring(1, 2) + "' AND VRPAUFLFD='" + EKP.Substring(3, 6) + "' AND VRPAUFPOS='" + EKP.Substring(9, 3) + "'";
            sort = " VRPREPOS ASC";
            rows = TBMain.Select(filter, sort);
            if (rows.Length == 0)
            {
                ((VVRPOS)MV).NEWBYEKP(dwEKP, dwZB, RENR);
            }
            if (IsFilterByVKP)
            {
                cvMaster.CustomFilter = filter;
            }           
            vwMaster.View.MoveCurrentToFirst();
        }
        
        public override void LOADBYEKP(string order)
        {
            if (order.Length < 11) return;

            filter = "VRPRENR='"+RENR+"' AND VRPAUFKRS='" + order.Substring(0, 2) + "' AND VRPAUFLFD='" + order.Substring(2, 6) + "' AND VRPAUFPOS='" + order.Substring(8, 3) + "'";
            sort = " VRPREPOS ASC";
            rows = TBMain.Select(filter, sort);
            if (rows.Length == 0)
            {
                base.LOADBYEKP(order);
            }

            if (IsFilterByVKP)
            {
                cvMaster.CustomFilter = filter;
            }
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.UpdateVRPOS(dwMaster,0);
            base.SaveCommand(sender, e);
        }
        private void SplitCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsNew)
                {
                    IsNew = true;
                    MessageBox.Show("请输入拆分比例");
                    return;
                }
                if (SQTY <= 0)
                    return;
                if (dwMaster == null)
                    return;
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwMaster.ItemArray;
                ((VVRPOS)MV).GetPOS(dwData, 9);
                double qty = System.Convert.ToDouble(dwMaster["VRPGESWERT"].ToString());
                double sqty = Helper.RoundX(qty * SQTY, 1);
                dwMaster["VRPGESWERT"] = sqty;
                dwData["VRPGESWERT"] = Helper.RoundX(qty - sqty, 1);
                for (int i = 1; i < 6; i++)
                {
                    dwData["VRPRABART" + i] = "   ";
                    dwData["VRPRABPROZ" + i] = "0";
                    dwData["VRPRABWERT" + i] = 0;
                    dwData["VRPZUART" + i] = "   ";
                    dwData["VRPZUABPROZ" + i] = "0";
                    dwData["VRPZUABWERT" + i] = 0;
                }
                TMV.Insert(dwData);
                TMV.UpdateVRPOS(dwMaster, 0);
                SQTY = -1;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 

        private void ShowAllCommand(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = null;
            IsFilterByVKP = !IsFilterByVKP;
            if (IsFilterByVKP)
            {
                cShow.Content = "按销售单";
            }
            else
            {
                cShow.Content = "全部";
            }
        }

        public override void LOADBYAUF()
        {
            Locate("KEY", ORDERNUM); 
        }
        /// <summary>
        /// 待处理现金不断变化
        /// 所有单行满足要求的都会被重新计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        /// <summary>
        /// 待处理现金不断变化
        /// 所有单行满足要求的都会被重新计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        DataRow dwPay = null;

        private void ConfirmVK04PaymentCommand(object sender, RoutedEventArgs e)
        {
            dwPay = null;
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一行发票");
                return;
            }
            filter = "VRPAUFKRS='" + dwMaster["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwMaster["VRPAUFLFD"].ToString()
                + "' AND VRPAUFPOS='" + dwMaster["VRPAUFPOS"].ToString() + "' AND VRPZOLLTNR LIKE '1:%'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                dwPay = row;
            }
            if (dwPay == null)
                return;
            IsVKPay = true;
        }

        private void ConfirmVVPaymentCommand(object sender, RoutedEventArgs e)
        {
            dwPay = null;
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一行发票");
                return;
            }
            filter = "VRPAUFKRS='" + dwMaster["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwMaster["VRPAUFLFD"].ToString()
              + "' AND VRPAUFPOS='" + dwMaster["VRPAUFPOS"].ToString() + "' AND VRPZOLLTNR LIKE '2:%'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                dwPay = row;
            }
            if (dwPay == null)
                return;
            IsVVPay = true;
        }

        private void ConfirmRESTPaymentCommand(object sender, RoutedEventArgs e)
        {
            dwPay = null;
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一行发票");
                return;
            }
            filter = "VRPAUFKRS='" + dwMaster["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwMaster["VRPAUFLFD"].ToString()
              + "' AND VRPAUFPOS='" + dwMaster["VRPAUFPOS"].ToString() + "' AND VRPZOLLTNR LIKE '3:%'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                dwPay = row;
            }
            if (dwPay == null)
                return;
            IsRest = true;
            //判断销售单状态
        }
        public override void OnPAIDVALUEChg(string value)
        {
            if (dwMaster == null)
                return;
            if (value == "")
                return;
            string paytype = value.Substring(0, 10).Trim();
            double v = double.Parse(value.Substring(10, 20).Trim());
            switch (paytype)
            {
                case "LINEPAY":
                    SVALUE = v;
                    LinePaid(dwPay, false, false, false);
                    break;
                #region ===VK04PAY======

                case "VK04PAY":
                    try
                    {
                        if (dwPay == null)
                            throw new Exception("没有需要支付的订金");
                        SVALUE = v;
                        filter = "VRPAUFKRS='" + dwPay["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwPay["VRPAUFLFD"].ToString() + "' AND VRPZOLLTNR LIKE '1:%'";
                        sort = " VRPAUFPOS ASC";
                        rows = TBMain.Select(filter);
                        foreach (DataRow dwLine in rows)
                        {
                            LinePaid(dwLine, true, false, false);// 
                        }
                        IsVKPay = false;
                    }
                    catch (Exception ex)
                    {
                        Tracer.Error(ex.StackTrace, ex);
                    }
                    break;
                #endregion
                #region ===VVPAY======
                case "VVPAY":
                    try
                    {
                        if (dwPay == null)
                            throw new Exception("没有需要支付的提货款");
                        SVALUE = v;
                        filter = "VRPAUFKRS='" + dwPay["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwPay["VRPAUFLFD"].ToString() + "' AND VRPZOLLTNR LIKE '2:%'";
                        sort = " VRPAUFPOS ASC";
                        rows = TBMain.Select(filter);
                        foreach (DataRow dwLine in rows)
                        {
                            LinePaid(dwLine, false, true, false);//
                        }
                        IsVVPay = false;
                    }
                    catch (Exception ex)
                    {
                        Tracer.Error(ex.StackTrace, ex);
                    }
                    break;
                #endregion
                #region ===RESTPAY======

                case "RESTPAY":
                    IsRest = true;
                    try
                    {
                        if (dwPay == null)
                            throw new Exception("没有需要支付的尾款");
                        SVALUE = v;
                        filter = "VRPAUFKRS='" + dwPay["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwPay["VRPAUFLFD"].ToString() + "' AND VRPZOLLTNR LIKE '3:%'";
                        sort = " VRPAUFPOS ASC";
                        rows = TBMain.Select(filter);
                        foreach (DataRow dwLine in rows)
                        {
                            LinePaid(dwLine, false, false, true);// 
                        }
                        IsRest = false;
                        //判断销售单状态
                    }
                    catch (Exception ex)
                    {
                        Tracer.Error(ex.StackTrace, ex);
                    }
                    break;
                #endregion
                default:
                    SVALUE = v;
                    if (dwPay == null)
                    {
                        MessageBox.Show("请选择一行发票");
                        return;
                    }
                    ManulPaid(dwPay, true, false, false);//   
                    IsMPay = false;
                    break;
            }
            base.OnPAIDVALUEChg(value);
            SVALUE = 0;
            IsNew = false;
            IsRest = false;
            IsVKPay = false;
            IsVVPay = false;

        }
        private void ManualPayment(object sender, RoutedEventArgs e)
        {

            if (dwMaster == null)
            {
                MessageBox.Show("请选择一行发票");
                return;
            }

            if (dwMaster == null)
                return;
            if (!IsMPay)
            { 
                SVALUE = 0;
                IsMPay = true;
                return;
            } 
            ManulPaid(dwMaster, true, false, false);//  
            IsMPay = false;
        }
    }
}